package de.teamgamma.cansat.groundstation.export;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Exporter for .kml
 * @author Marc Huisinga
 *
 */
public class KmlExporter implements Exporter {

	private final String markerName = "Apollo 12 Location";
	private final String description = "The Apollo 12 from Team Gamma landed here";
	
	@Override
	public void export(ExportableData input, File output) {
		File outputFile = new File(output.getAbsolutePath() + ".kml");
		boolean mkFileSucceeded = false;
		try {
			//Creates the new kml file
			mkFileSucceeded = outputFile.createNewFile();
		} catch (IOException e1) {
			System.err.println("Invalid output file path!");
		}
		if(mkFileSucceeded) {
			//gets the altitude, longitude and latitude from the exportable Data
			String[] altitude = input.getDataOfKey("altitude");
			String[] longitude = input.getGpsData().getDataOfKey("latitude");
			String[] latitude = input.getGpsData().getDataOfKey("longitude"); 	
			
			double latitudeValue;
			double longitudeValue;
			
			try {
				BufferedWriter currentWriter = new BufferedWriter(new FileWriter(outputFile));
				//Writes the xml syntax into the kml file
				currentWriter.write("<?xml version='1.0' encoding='UTF-8'?><kml xmlns='http://www.opengis.net/kml/2.2'><Document> <name>" + markerName + "</name><description>" + description + "</description> <Style id='yellowLineGreenPoly'> <LineStyle> <color>7f00ffff</color> <width>4</width> </LineStyle> <PolyStyle> <color>7f00ff00</color> </PolyStyle> </Style>  <Placemark> <name>Absolute Extruded</name> <description> </description> <styleUrl>#yellowLineGreenPoly</styleUrl> <LineString> <extrude>1</extrude> <tessellate>1</tessellate> <altitudeMode>absolute</altitudeMode> <coordinates>");
				//for-loop to write all coordinates into the file and create the route
				for(int i = 0; i < altitude.length; i++) {
					//Format for LAT is ddmm.mmmm             dd                             mm.mmmm
					latitudeValue = Double.parseDouble(latitude[i].substring(0, 3)) + (Double.parseDouble(latitude[i].substring(3,10)) / 60);					
					
					//Format for LONG is dddmm.mmmm                  ddd                         mm.mmmm
					longitudeValue = Double.parseDouble(longitude[i].substring(0, 2)) +(Double.parseDouble(longitude[i].substring(2, 9)) / 60);
					
					// [n] W => -[n] for decimal
					if (longitude[i].endsWith("W")) {
						longitudeValue *= -1;
					}
					
					// [n] S => -[n] for decimal
					if (latitude[i].endsWith("S")) {
						latitudeValue *= -1;
					}
					
					//writing the actual long-, lati- and altitude values into the file
					currentWriter.write(latitudeValue + "," + longitudeValue + "," + altitude[i] + System.getProperty("line.separator"));
				}
				//writes the end of the xml syntax into the file
				currentWriter.write("</coordinates> </LineString> </Placemark> </Document> </kml>");
				currentWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	
}